package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.dto.R;
import com.qf.ssm.pojo.User;
import com.qf.ssm.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/user")
    public R addUser(@RequestBody User user) throws Exception {
        userService.addUser(user);
        return R.setOK();
    }

    @GetMapping("/users")
    public R findAllUserByPage(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "2") int limit){
        PageInfo<User> pageInfo = userService.findAllUserByPage(page,limit);
        return R.setOK(pageInfo);
    }

    @DeleteMapping("/users")
    public R deleteUsersByIds(Long[]ids){
        userService.deleteUsersByIds(ids);
        return R.setOK();
    }

    @PutMapping("/user")
    public R updateUser(@RequestBody User user){
        int result = userService.updateUser(user);
        return R.setOK();
    }
}